package com.esiran.payadmin.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.esiran.payadmin.dao.UserCategoryDao;
import com.esiran.payadmin.exception.APIRespException;
import com.esiran.payadmin.model.UserCategory;
import com.esiran.payadmin.service.UserCategoryService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserCategoryServiceImpl extends
        ServiceImpl<UserCategoryDao, UserCategory>
        implements UserCategoryService {
    @Override
    public void create(UserCategory userCategory) throws APIRespException {
        Wrapper<UserCategory> wrapper = new EntityWrapper<>();
        wrapper.where("name = {0}", userCategory.getName())
                .and("ownerId",userCategory.getOwnerId());
        UserCategory uc = selectOne(wrapper);
        if (uc != null){
            throw new APIRespException("分类名已存在", 1007);
        }
        baseMapper.insert(userCategory);
    }

    @Override
    public Page<UserCategory> getUserCategoryListById(Long userId, Page<UserCategory> page) {
        Wrapper<UserCategory> wrapper = new EntityWrapper<>();
        wrapper.where("ownerId = {0}", userId).orderBy("orders desc, createTime asc");
        List<UserCategory> records = baseMapper.selectPage(page,wrapper);
        page.setRecords(records);

        return page;
    }

}
